<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<base href="<%=basePath%>">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css"
	href="<%=basePath%>/easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css"
	href="<%=basePath%>/easyui/themes/icon.css" />
<link rel="stylesheet" type="text/css"
	href="<%=basePath%>/css/index.css" />
<title>index Page</title>

</head>

<body>
	<table id="order"></table>

	<div id="order_tool" style="padding:5px;">
		<div style="margin-bottom:5px;">
			<a href="#" class="easyui-linkbutton" iconCls="icon-export"plain="true" onclick="order_tool.exportExcel();">导出Excel</a> 
			<a href="#" class="easyui-linkbutton" iconCls="icon-edit-new"plain="true" onclick="order_tool.edit();">修改</a> 
			<a href="#"class="easyui-linkbutton" iconCls="icon-delete-new" plain="true"onclick="order_tool.remove();">删除</a> 
			<a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true"onclick="order_tool.reload();">刷新</a> 
			<a href="#" class="easyui-linkbutton" iconCls="icon-check_pass" plain="true"onclick="order_tool.cancle();">同意取消</a> 
			<a href="#"class="easyui-linkbutton" iconCls="icon-redo" plain="true"onclick="order_tool.redo();">取消选择</a> 
		</div>

		<div style="padding:0 0 0 7px;color:#333;">
			<form id="search">
				<p>
					订单编号：<input type="text" class="textbox" name="orderIdSearch"style="width:110px"> 
					下单客户：<input type="text"class="textbox" name="userNameSearch" style="width:110px">
					下单酒店：<input type="text"class="textbox" name="hotelNameSearch" style="width:110px">
					消费价格从：<input type="text" class="textbox" name="price_from"style="width:110px"> 
					到：<input type="text" class="textbox" name="price_to"style="width:110px"> 
				</p>
				<p>	
					下单时间从：<input type="text"name="date_from" class="easyui-datebox" editable="false"style="width:105px"> 
					&nbsp;&nbsp;&nbsp;到：<input type="text" name="date_to"class="easyui-datebox" editable="false" style="width:115px">
					订单状态：
					<select id="stateSearch" style="width:110px">
						<option/>
						<option value="0">已过期</option>
						<option value="1">待取消</option>
						<option value="2">已取消</option>
						<option value="3">待支付</option>
						<option value="4">已支付</option>
					</select>
					&nbsp;下单评论：<input type="text"class="textbox" name="commentSearch" style="width:110px">
					<a href="#" class="easyui-linkbutton" iconCls="icon-search"onclick="order_tool.search();">查询</a>
				</p>
			</form>
		</div>
	</div>

	<form id="order_edit"style="text-align:center; margin:0;padding:5px 0 0 25px;color:#333;">
		<input type="hidden" id="createTime_edit" name="createTime_edit" class="textbox" style="width:200px;"> 
		<p>
			订单编号：<input readonly="readonly" type="text" id="orderId" name="orderId"class="textbox" style="width:200px;">
		</p>
		<p>
			用户名称：<input readonly="readonly"  type="text" name="userName" id = "userName" class="textbox" style="width:200px;">
		</p>
		<p>
			酒店名称：<input readonly="readonly"  type="text" name="hotelName" class="textbox"style="width:200px;">
		</p>
		<p>
			开始时间：<input id="beginTime_edit" name="beginTime_edit"style="width:205px">
		</p>
		<p>
			结束时间：<input id="endTime_edit" name="endTime_edit"style="width:205px">
		</p>
		<p>
			消费信息：<input type="text" name="orderContext" class="textbox"style="width:200px;">
		</p>
		<p>
			消费金额：<input type="text" name="price" class="textbox"style="width:200px;">
		</p>
		<p>
			订单状态： <select id="state" style="width:200px" type="text">
				<option />
				<option value="0">已过期</option>
				<option value="1">待取消</option>
				<option value="2">已取消</option>
				<option value="3">待支付</option>
				<option value="4">已支付</option>
			</select>
		</p>
		<p>
			下单评论：<input type="text" readonly="readonly" name="comment_edit" class="textbox" style="width:200px;">
		</p>
	</form>
	<script type="text/javascript" src="<%=basePath%>/easyui/jquery.min.js"></script>
	<script type="text/javascript"src="<%=basePath%>/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript"src="<%=basePath%>/easyui/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript">
	$.extend($.fn.validatebox.defaults.rules, {
		 	isNum: {
			    validator: function(value){
				    var rex = /^\d{0,9}$/;
				    if(rex.test(value)) {
				      return true;
				    } else {
				       return false;
				    }
			    },
			    message: '请输入整数'
		 	}
		 });
		 
		$(function () {
			$('#stateSearch').combobox({
				editable : false,
				panelHeight:'auto'
			});
			$('#order').datagrid({
				url : 'orderAction!getOrderList',
				fit : true,
				fitColumns : true,
				striped : true,
				rownumbers : true,
				border : false,
				pagination : true,
				pageSize : 10,
				pageList : [5, 10, 15, 20, 25],
				pageNumber : 1,
				sortName : 'createTime',
				sortOrder : 'desc',
				toolbar : '#order_tool',
				remoteSort:	false,
				columns : [[
					{
						field : 'ck',
						title : '自动编号',
						align:'center',
						width : 100,
						checkbox : true,
					},
					{
						field : 'orderId',
						title : '订单编号',
						align:'center',
						width : 100,
						sortable : true,
					},
					{
						field : 'userName',
						title : '下单用户',
						width : 100,
						align:'center',
						sortable : true
					},
					{
						field : 'hotelName',
						title : '下单宾馆',
						width : 100,
						align:'center',
						sortable : true
					},
					{
						field : 'beginTime',
						title : '开始日期',
						width : 100,
						align:'center',
		  				sortable : true,
		  				formatter : function (value, rowData, rowIndex) {
							  if(value != null){
							  		var date = new Date(value);
							  	  	return  date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
							  }
						}
					},
					{
						field : 'endTime',
						title : '结束日期',
						width : 100,
						align:'center',
		  				sortable : true,
		  				formatter : function (value, rowData, rowIndex) {
							  if(value != null){
							  		var date = new Date(value);
							  	  	return  date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
							  }
						}
					},
					{
						field : 'orderContext',
						title : '消费说明',
						align:'center',
						width : 100,
						sortable : true,
					},
					{
						field : 'price',
						title : '消费金额',
						align:'center',
						width : 100,
						sortable : true,
					},
					{
						field : 'createTime',
						title : '下单日期',
						width : 100,
						align:'center',
		  				sortable : true,
		  				formatter : function (value, rowData, rowIndex) {
							  if(value != null){
							  		var date = new Date(value);
							  	  	return  date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
							  }
						}
					},
					{
						field : 'state',
						title : '下单状态',
						width : 100,
		  				sortable : true,
		  				align:'center',
		  				formatter : function (value, rowData, rowIndex) {
						  if(value == 0){
						  	  return '已过期';
						  }else if(value == 1){
						  	  return '待取消';
						  }else if(value == 2){
							  return '已取消';
						  }else if(value == 3){
						  	  return '待支付';
						  }else if(value == 4){
						 	  return '已支付';
						  }
						}
					},
					{
						field : 'comment',
						title : '下单评论',
						align:'center',
						width : 100,
						sortable : true,
					},
				]]
			});
		    
			//修改
			$('#order_edit').dialog({
				width : 350,
				title : '修改订单信息',
				modal : true,
				closed : true,
				iconCls : 'icon-user-add',
				buttons : [{
					text : '提交',
					iconCls : 'icon-edit-new',
					handler : function () {
						if ($('#order_edit').form('validate')) {
							$.ajax({
									url : 'orderAction!updateOrder',
									type : 'post',
									data : {
										orderId   : $('input[name="orderId"]').val(),
										state : $('#state').combobox('getValue'),
										price : $('input[name="price"]').val(), 
										orderContext : $('input[name="orderContext"]').val(),
										beginTime : $.trim($('input[name="beginTime_edit"]').val()),
										endTime : $.trim($('input[name="endTime_edit"]').val()),
									},
									beforeSend : function () {
										$.messager.progress({
											text : '正在修改中...'
										});
									},
									success : function (data, response, status) {
										
										$.messager.progress('close');
										if (data > 0) {
											$.messager.show({
												title : '提示',
												msg : '修改订单成功'
											});
											$('#order_edit').dialog('close').form('reset');
											$('#order').datagrid('reload');
										} else {
											$.messager.alert('修改失败！', '未知错误导致失败，请重试！', 'warning');
										}
									}
								});
						}
					}
				},{
					text : '取消',
					iconCls : 'icon-redo',
					handler : function () {
						$('#order_edit').dialog('close').form('reset');
					}
				}]
			});
			
			$('#beginTime_edit').datebox({
				require  : true,
				editable : false,
				formatter: function(date){ 
					 return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
				 }
			});
			
			$('#endTime_edit').datebox({
			     require  : true,
				 editable : false,
				 formatter: function(date){ 
					 return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
				 }
			});
			
			$('input[name="price"]').validatebox({
				required : true,
				validType :  [
					'isNum'
				],
				missingMessage : '请输入所要修改的消费价格',
			});
			
			$('#state').combobox({
				require : true,
				editable:false,
				panelHeight:'auto'
			});
			//按钮事件
			order_tool = {
				//刷新
				reload : function () {
					$('#order').datagrid('reload');
				},
				//撤销
				redo : function () {
					$('#order').datagrid('unselectAll');
					$('#search').form('clear');
				},
				//搜索
				search : function () {
					$('#order').datagrid('load', {
						orderIdSearch : $.trim($('input[name="orderIdSearch"]').val()),
						userNameSearch : $.trim($('input[name="userNameSearch"]').val()),
						hotelNameSearch : $.trim($('input[name="hotelNameSearch"]').val()),
						price_to :$.trim($('input[name="price_to"]').val()),
						price_from :$.trim($('input[name="price_from"]').val()),
						date_from : $.trim($('input[name="date_from"]').val()),
						date_to : $.trim($('input[name="date_to"]').val()),
						stateSearch : $.trim($('#stateSearch').combobox('getValue')),
						commentSearch : $.trim($('input[name="commentSearch"]').val()),
					});
				},
				exportExcel : function () {
					var rows = $('#order').datagrid('getSelections');
					var showMsg = "";
					if (rows.length > 0) {
						showMsg = "您确定要导出选择的记录吗？";
					} else {
						showMsg = "您没有选择任何记录，是要导出页面展示的所有记录吗？";
					}
					$.messager.confirm('确定操作', showMsg, function (flag) {
						if (flag) {
							var ids = [];
							for (var i = 0; i < rows.length; i ++) {
								ids.push(rows[i].orderId);
							}
							var orderIdSearch = $.trim($('input[name="orderIdSearch"]').val());
							var userNameSearch = $.trim($('input[name="userNameSearch"]').val());
							var hotelNameSearch = $.trim($('input[name="hotelNameSearch"]').val());
							var price_to =$.trim($('input[name="price_to"]').val());
							var price_from =$.trim($('input[name="price_from"]').val());
							var date_from = $.trim($('input[name="date_from"]').val());
							var date_to = $.trim($('input[name="date_to"]').val());
							var stateSearch = $.trim($('#stateSearch').combobox('getValue'));
							var commentSearch = $.trim($('input[name="commentSearch"]').val());
							window.location.href = '<%=path%>/orderAction!exportExcel?ids=' + ids +
								 '&orderIdSearch =' + orderIdSearch +
								 '&userNameSearch =' + userNameSearch +
								 '&hotelNameSearch=' + hotelNameSearch +
								 '&price_to ='+price_to+
								 '&price_from='+price_from+
								 '&date_from='+date_from+
								 '&date_to='+date_to+
								 '&stateSearch='+stateSearch+
								 '&commentSearch='+commentSearch
								 ;
						}
					});
					
				},
				//删除
				remove : function () {
					var rows = $('#order').datagrid('getSelections');
					if (rows.length > 0) {
						$.messager.confirm('确定操作', '您确定要删除所选的记录吗？', function (flag) {
							if (flag) {
								var ids = [];
								for (var i = 0; i < rows.length; i ++) {
									ids.push(rows[i].orderId);
								}
								$.ajax({
									type : 'POST',
									url : 'orderAction!deleteOrders',
									data : {
										ids : ids.join(',')
									},
									beforeSend : function () {
										$('#order').datagrid('loading');
									},
									success : function (data) {
										if (data > 0) {
											$('#order').datagrid('reload');
											$('#order').datagrid('unselectAll');
											$.messager.show({
												title : '提示',
												msg : data + '个订单被删除成功！',
											});
										} else{
											$('#order').datagrid('reload');
											$('#order').datagrid('unselectAll');
											$.messager.alert('删除失败！', '未知错误导致失败，请重试！', 'warning');
										}
									}
								});
							}
						});
					} else {
						$.messager.alert('提示', '请选择要删除的记录！', 'info');
					}
				},
				//编辑
				edit : function () {
						var rows = $('#order').datagrid('getSelections');
						if (rows.length > 1) {
							$.messager.alert('警告操作！', '编辑记录只能选定一条数据！', 'warning');
						} else if (rows.length == 1) {
							$.ajax({
								url : 'orderAction!getOrderByOrderId',
								type : 'post',
								data : {
									orderId : rows[0].orderId
								},
								beforeSend : function () {
									$.messager.progress({
										text : '正在获取中...'
									});
								},
								success : function (data, response, status) {
									$.messager.progress('close');
									if (data) {
										var obj = $.parseJSON(data);
										var date = new Date(obj.order.beginTime);
										var tt = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
										if(tt == 'NaN-NaN-NaN NaN:NaN:NaN'){
											tt = '';
										}
										$('#beginTime_edit').datebox('setValue',tt);
										
										var date0 = new Date(obj.order.endTime);
										var tt0 = date0.getFullYear() + '-' + (date0.getMonth() + 1) + '-' + date0.getDate() + ' ' + date0.getHours() + ":" + date0.getMinutes() + ":" + date0.getSeconds();
										if(tt0 == 'NaN-NaN-NaN NaN:NaN:NaN'){
											tt0 = '';
										}
										$('#endTime_edit').datebox('setValue',tt0);
										
										var date1 = new Date(obj.order.createTime);
										var tt1 = date1.getFullYear() + '-' + (date1.getMonth() + 1) + '-' + date1.getDate() + ' ' + date1.getHours() + ":" + date1.getMinutes() + ":" + date1.getSeconds();
										
										$('#order_edit').form('load', {
											createTime_edit : tt1,
											orderId        : obj.order.orderId,
											userName       : obj.userName,
											hotelName      : obj.hotelName,
											price          : obj.order.price,
											orderContext   : obj.order.orderContext,
											comment_edit   : obj.order.comment
										}).dialog('open');
									   $('#state').combobox('setValue', obj.order.state);
									} else {
										$.messager.alert('获取失败！', '未知错误导致失败，请重试！', 'warning');
									}
								}
							});
						} else if (rows.length == 0) {
							$.messager.alert('警告操作！', '编辑记录至少选定一条数据！', 'warning');
						}
					},
					cancle : function(){
						var rows = $('#order').datagrid('getSelections');
						if (rows.length > 0) {
							var ids = [];
							for (var i = 0; i < rows.length; i ++) {
								if(rows[i].state != 1){
									$.messager.alert('提示', '请选择记录存在不是待取消的订单！', 'info');
									return;
								}
								ids.push(rows[i].orderId);
							}
							$.messager.confirm('确定操作', '您确定要执行所选的记录吗？', function (flag) {
								if (flag) {
									$.ajax({
										type : 'POST',
										url : 'orderAction!cancleOrders',
										data : {
											ids : ids.join(',')
										},
										beforeSend : function () {
											$('#order').datagrid('loading');
										},
										success : function (data) {
											if (data > 0) {
												$('#order').datagrid('reload');
												$('#order').datagrid('unselectAll');
												$.messager.show({
													title : '提示',
													msg : data + '个订单被取消成功！',
												});
											} else{
												$('#order').datagrid('reload');
												$('#order').datagrid('unselectAll');
												$.messager.alert('取消失败！', '未知错误导致失败，请重试！', 'warning');
											}
										}
									});
								}
							});
						} else {
							$.messager.alert('提示', '请选择要取消的记录！', 'info');
						}
					}
				};
		});
		</script>
</body>
</html>